﻿/**********************************************************************************
 * Project Name :   
 * Remarks      :  
 * Class Name   :  UtilityCheckData   
 * Version      :  v1.0.0.0 
 * Author       :  Joey Liang 
 * Description  : 
 * Created Time :  2013/5/22 0:24:49
 * Update Man   :  Joey Liang
 * Update Time  :  2013/5/22 0:24:49
************************************************************************/

using System;
using System.Collections.Generic;

namespace JoeyLibs.Utility
{
    /// <summary>
    /// 判断数据帮助类
    /// </summary>
    public class UtilityCheckData
    {
        /// <summary>
        /// 判断集合对象是否为空或无元素，如果是则返回true
        /// </summary>
        public static bool IsCollectionEmpty<T>(ICollection<T> collection)
        {
            return collection == null || collection.Count < 1;
        }

        #region String
        /// <summary>
        /// 验证字符串是否全部包含字符串集
        /// </summary>
        /// <param name="master">待验证 原字符串</param>
        /// <param name="validates">待验证包含的字符串 集合</param>
        /// <returns>只要有一个字符串不被包含，则返回 false</returns>
        public static bool StringContains(string master, params string[] validates)
        {
            bool result = false;
            if (string.IsNullOrEmpty(master) || validates == null || validates.Length < 1)
            {
                return result;
            }

            result = true;
            foreach (string item in validates)
            {
                if (!master.Contains(item))
                {
                    result = false;
                    return result;
                }
            }

            return result;
        }

        /// <summary>
        /// 验证字符串集合皆不为空,即全部不为空
        /// </summary>
        /// <param name="isTrim">是否在验证前去掉空格字符</param>
        /// <param name="strs">要判断的字符串.</param>
        /// <returns>返回是否包含有Null或Empty的字符串</returns>
        public static bool StringsIsNotNullAndEmpty(bool isTrim = false, params string[] strs)
        {
            bool resutl = false;
            if (strs == null || strs.Length <= 0)
            {
                return resutl;
            }

            foreach (string str in strs)
            {
                if (string.IsNullOrEmpty(str) || (isTrim && string.IsNullOrEmpty(str.Trim())))
                {
                    return resutl;
                }
            }

            resutl = true;
            return resutl;
        }

        /// <summary>
        /// 对比object的字符是否与string相同
        /// </summary>
        /// <param name="obj">The obj.</param>
        /// <param name="str">The STR.</param>
        /// <param name="isMatchCase">是否忽略大小写</param>
        /// <param name="isTrim">是否去掉前后空格再比较</param>
        /// <returns>完全相同返回true</returns>
        public static bool ObjectEqualString(object obj, string str, bool isTrim, bool isMatchCase = false)
        {
            bool result = true;
            if (obj == null && string.IsNullOrEmpty(str))
            {
                return result;
            }

            string objstr = UtilityConvert.ObjectToString(obj);
            return StringEqual(objstr, str, isTrim, isMatchCase);
        }

        /// <summary>
        /// 判断字符串是否相等，当值为null、"",string.Empty均认为相等
        /// </summary>
        /// <param name="str1">比较的字符串.</param>
        /// <param name="str2">比较的字符串.</param>
        /// <param name="isMatchCase">是否忽略大小写</param>
        /// <param name="isTrim">是否去掉前后空格再比较</param>
        public static bool StringEqual(string str1, string str2, bool isTrim = false, bool isMatchCase = false)
        {
            bool result = true;
            if (string.IsNullOrEmpty(str1) && string.IsNullOrEmpty(str2))
            {
                return result;
            }

            if (!string.IsNullOrEmpty(str1))
            {
                if (isTrim)
                    str1 = str1.Trim();

                if (isMatchCase)
                    str1 = str1.ToLower();
            }

            if (!string.IsNullOrEmpty(str2))
            {
                if (isTrim)
                    str2 = str2.Trim();

                if (isMatchCase)
                    str2 = str2.ToLower();
            }

            if (string.IsNullOrEmpty(str1) && string.IsNullOrEmpty(str2))
            {
                return result;
            }

            return str1 == str2;
        }
        #endregion
    }
}
